/*
 * psapp.h
 *
 *  Created on: 2011-5-17
 *      Author: chenl
 */

#ifndef PSAPP_H_
#define PSAPP_H_

#include "task_wrapper.h"
#include "loki/Singleton.h"
#include "scheduler.h"
#include "global.h"
#include <pthread.h>

class CPSApp
{
public:
	CPSApp();
	~CPSApp();
public:
	int32_t Initialize();
	int32_t Uninitialize();

	int32_t InitScheduler();
	int32_t DelScheduler();

	void DoSchedule();

	void UpDateTaskTimes()
	{
		pScheduler_->scheduler_tick();
	}

	void SetUpdated()
	{
		pthread_mutex_lock(&mutex_);
		bIsModified_ = false;
		pthread_mutex_unlock(&mutex_);
	}
	void SetModified()
	{
		pthread_mutex_lock(&mutex_);
		bIsModified_ = true;
		pthread_mutex_unlock(&mutex_);
	}
	bool IsUpdated()
	{
		pthread_mutex_lock(&mutex_);
		bool bIsUpdated = bIsModified_ ? false : true;
		pthread_mutex_unlock(&mutex_);
		return bIsUpdated;
	}
	bool IsModified()
	{
		pthread_mutex_lock(&mutex_);
		bool bIsModified = bIsModified_;
		pthread_mutex_unlock(&mutex_);
		return bIsModified;
	}

public:
	CPSThreadLog& logEngine;
private:
	pthread_mutex_t	mutex_;
	IScheduler* pScheduler_;
	volatile bool bIsModified_;
};

typedef Loki::SingletonHolder<CPSApp> 	PSApp;
#define CREATE_PSAPP_INSTANCE			PSApp::Instance
#define GET_PSAPP_INSTANCE				PSApp::Instance

#endif /* PSAPP_H_ */
